package com.jdaoyun.a_preAid.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdaoyun.a_preAid.entity.GPSStatusModel;
import com.jdaoyun.a_preAid.entity.SysParameterInfo;
import com.jdaoyun.a_preAid.mapper.ParameterMapper;
import com.jdaoyun.a_preAid.service.ParameterService;
import com.jdaoyun.config.ThirdPartyConfigure;
import com.jdaoyun.constant.ParameterConst;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;


/**
 * 系统参数(SysParameter)表服务接口
 *
 * @author makejava
 * @since 2025-08-20 19:56:12
 */
@Service
public class ParameterServiceImpl extends ServiceImpl<ParameterMapper, SysParameterInfo> implements ParameterService {

    @Resource
    private ParameterMapper parameterMapper;

    @Override
    public SysParameterInfo getByName(String name, Integer tenantId) {
        SysParameterInfo queryInfo = new SysParameterInfo();
        queryInfo.setName(name);
        queryInfo.setTenantId(tenantId);
        SysParameterInfo sysParameterInfo = parameterMapper.selectOne(new QueryWrapper<SysParameterInfo>().setEntity(queryInfo));
        return sysParameterInfo;
    }


    @Override
    public String getStringValue(String name) {

        String result = null;

        SysParameterInfo sysParameterInfo = this.getByName(name, null);
        if (sysParameterInfo == null) {
            result = null;
        } else {
            result = sysParameterInfo.getValue();
        }

        return result;
    }

    @Override
    public Integer getIntValueByName(String name,Integer tenantId) {

        Integer result = null;

        SysParameterInfo sysParameterInfo = this.getByName(name, tenantId);
        if (sysParameterInfo == null) {
            result = null;
        } else {
            try {
                result = Integer.valueOf(sysParameterInfo.getValue());
            } catch (NumberFormatException e) {
                e.printStackTrace();
                result = null;
            }
        }

        return result;
    }

    @Override
    public List<SysParameterInfo> getAllList() {
        return parameterMapper.selectList(new QueryWrapper<SysParameterInfo>());
    }

    @Override
    public String getVersion() {
        SysParameterInfo sysParameterInfo = parameterMapper.selectOne(new QueryWrapper<SysParameterInfo>().eq("name", ParameterConst.VERSION));
        return sysParameterInfo.getValue();
    }

    @Override
    public String setValueByName(String name, String value) {
        SysParameterInfo sysParameterInfo = this.getOne(new LambdaQueryWrapper<SysParameterInfo>().eq(SysParameterInfo::getName, name), false);

        if (Objects.nonNull(sysParameterInfo)) {
            sysParameterInfo.setValue(value);
            this.updateById(sysParameterInfo);
        }

        return sysParameterInfo.getValue();
    }
    @Autowired
    private ThirdPartyConfigure thirdPartyConfigure;
    @Override
    public List<GPSStatusModel> getGPSStatusModel(List<String> deviceFeature) {
//        String url = thirdPartyConfigure.getAdress() + thirdPartyConfigure.getGetGpsStatus();
//        Result<List<GPSStatusModel>> result = restTemplateUtils.post(url, deviceFeature, new ParameterizedTypeReference<Result<List<GPSStatusModel>>>() {
//        }).getBody();
//        List<GPSStatusModel> data = result.getData();
//        return data;
        return null;
    }
}
